<?php
namespace App\Http\Controllers\Front;

use Illuminate\Http\Request;
//require_once __DIR__.'/../../../../app/Utilitys/Alipay/aop/AopEncrypt.php';
//require_once __DIR__.'/../../../../app/Utilitys/Alipay/aop/EncryptParseItem.php';
//require_once __DIR__.'/../../../../app/Utilitys/Alipay/aop/EncryptResponseData.php';
//require_once __DIR__.'/../../../../app/Utilitys/Alipay/aop/SignData.php';
//require_once __DIR__.'/../../../../app/Utilitys/Alipay/aop/AlipayConfig.php';
require_once __DIR__.'/../../../../app/Utilitys/Alipay/aop/AopClient.php';
require_once __DIR__.'/../../../../app/Utilitys/Alipay/aop/request/AlipayTradePagePayRequest.php';
require_once __DIR__.'/../../../../app/Utilitys/Alipay/aop/request/AlipayTradeAppPayRequest.php';
use App\Service\Front\PayService;

class PayZfbController1115 extends BaseController
{
    //支付

    //必须的参数
    protected $app_id = '2021004194624726';
    protected $rsaPrivateKeyFilePath = '';
    protected $rsaPrivateKey = 'MIIEowIBAAKCAQEAnLgv5fv2ernleOuSR2val8nmWWp42XPZrD9HNpgR/rDy7D/u2QDXzEbKyNZhe26vTo7uxC9vVczRD5iwBCGXLdrCSFPZdbtuqHH0MTb6WEVE3hhPWZ89WMN5TiQwtMjdBLBeNgXC2JyP6uxAItQgpQnPjwcVua5UsUNG2P89/g6YISfrYBpYZVF57ennlePPwhHYnYlTrdRlPP39JOEiz5YfBIlm3ZTFMSfSgHQKWVipJuR+/zhKb53msmZqs18RNpGm3TEedfgQYMOd6z5Ybl6s1oEd9o5n2VDzRrLXKrA7dKrEKjeEGztZNXV6LvShU2kq+j1/mauuaS7XBLAW/QIDAQABAoIBABz5IEHQdxzxSS81vfFeSydMuD1HpdftxwxFFpFNQ+ZmKELs/uc3dK3bWJoQqCz9pYNMgW/bpSWjm/LaM5BP9XcQvmJIvic545dYmeCPXXD8PWMh82mKOvkyCjccEbUafWHkiUZLgl4xc5nm22SU+wQH8Ets7+NIC3hKC8sKRRe2p7RBj6yv7M1OdUUm+a7hbyE95YSLx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YKmb81fwp2mEOKHivAJX7X8Givpxg55y5h1+YW4K66MtvlCiIGAB9BJrV1bAkAACkKcdV4JxuH7Magq2aQu5Qhfn+xJt6EkcLlQMpV4daKOdnriIicpVnsQuNiNt2TNskGxgvlTUO10s1qf6Gj/q6eu+Y2/DbhnyJPsHaqfmpqHnaOVi3jqDBHEe/7xsBZEEPETWmqgH9I6/BEUcMGDnPD/B3y0tbGCkZNi+TZVBnmW9cVrWTgVy6J4CwXKGkTapdREw0exozHnmgOsL7KuOaqBE8NDanFiHtMwuXyv6RIqJ6Yb5KkWkFSXgJalmL4ddEaIHz0mTiI3Bcp/i/Co+wIDAQAB';
    protected $alipayrsaPublicKey = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnLgv5fv2ernleOuSR2val8nmWWp42XPZrD9HNpgR/rDy7D/u2QDXzEbKyNZhe26vTo7uxC9vVczRD5iwBCGXLdrCSFPZdbtuqHH0MTb6WEVE3hhPWZ89WMN5TiQwtMjdBLBeNgXC2JyP6uxAItQgpQnPjwcVua5UsUNG2P89/g6YISfrYBpYZVF57ennlePPwhHYnYlTrdRlPP39JOEiz5YfBIlm3ZTFMSfSgHQKWVipJuR+/zhKb53msmZqs18RNpGm3TEedfgQYMOd6z5Ybl6s1oEd9o5n2VDzRrLXKrA7dKrEKjeEGztZNXV6LvShU2kq+j1/mauuaS7XBLAW/QIDAQAB';
    protected $notify_url = ''; //通知接口

    public function __construct()
    {
        $this->rsaPrivateKeyFilePath = 'zfb/rsaPrivateKeyFilePath.txt';
        $this->notify_url = env('DOMAIN') . '/api/front/pay/zfb/notice';
        $this->db = new PayService();
    }

    //测试
    public function test(){
        $aop = new \AopClient();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $this->app_id;
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'utf-8';
        $aop->format = 'json';

        $reqObj = new \AlipayTradePagePayRequest();
        $reqObj->setBizContent(json_encode([
            'out_trade_no' => (string) time(),
            'total_amount' => '0.01',
            'subject' => 'order',
            'product_code' => 'FAST_INSTANT_TRADE_PAY',
        ], JSON_UNESCAPED_UNICODE));
//        $reqObj->setBizContent("{" .
//            "\"timeout_express\":\"90m\"," .
//            "\"total_amount\":\"9.00\"," .
//            "\"product_code\":\"QUICK_MSECURITY_PAY\"," .
//            "\"body\":\"Iphone6 16G\"," .
//            "\"subject\":\"大乐透\"," .
//            "\"out_trade_no\":\"70501111111S001111119\"," .
//            "\"time_expire\":\"2016-12-31 10:05\"," .
//            "\"goods_type\":\"0\"," .
//            "\"promo_params\":\"{\\\"storeIdType\\\":\\\"1\\\"}\"," .
//            "\"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\"," .
//            "\"extend_params\":{" .
//            "\"sys_service_provider_id\":\"2088511833207846\"," .
//            "\"hb_fq_num\":\"3\"," .
//            "\"hb_fq_seller_percent\":\"100\"," .
//            "\"industry_reflux_info\":\"{\\\\\\\"scene_code\\\\\\\":\\\\\\\"metro_tradeorder\\\\\\\",\\\\\\\"channel\\\\\\\":\\\\\\\"xxxx\\\\\\\",\\\\\\\"scene_data\\\\\\\":{\\\\\\\"asset_name\\\\\\\":\\\\\\\"ALIPAY\\\\\\\"}}\"," .
//            "\"card_type\":\"S0JP0000\"" .
//            "    }," .
//            "\"merchant_order_no\":\"20161008001\"," .
//            "\"enable_pay_channels\":\"pcredit,moneyFund,debitCardExpress\"," .
//            "\"store_id\":\"NJ_001\"," .
//            "\"specified_channel\":\"pcredit\"," .
//            "\"disable_pay_channels\":\"pcredit,moneyFund,debitCardExpress\"," .
//            "      \"goods_detail\":[{" .
//            "        \"goods_id\":\"apple-01\"," .
//            "\"alipay_goods_id\":\"20010001\"," .
//            "\"goods_name\":\"ipad\"," .
//            "\"quantity\":1," .
//            "\"price\":2000," .
//            "\"goods_category\":\"34543238\"," .
//            "\"categories_tree\":\"124868003|126232002|126252004\"," .
//            "\"body\":\"特价手机\"," .
//            "\"show_url\":\"http://www.alipay.com/xxx.jpg\"" .
//            "        }]," .
//            "\"ext_user_info\":{" .
//            "\"name\":\"李明\"," .
//            "\"mobile\":\"16587658765\"," .
//            "\"cert_type\":\"IDENTITY_CARD\"," .
//            "\"cert_no\":\"362334768769238881\"," .
//            "\"min_age\":\"18\"," .
//            "\"fix_buyer\":\"F\"," .
//            "\"need_check_info\":\"F\"" .
//            "    }," .
//            "\"business_params\":\"{\\\"data\\\":\\\"123\\\"}\"," .
//            "\"agreement_sign_params\":{" .
//            "\"personal_product_code\":\"CYCLE_PAY_AUTH_P\"," .
//            "\"sign_scene\":\"INDUSTRY|DIGITAL_MEDIA\"," .
//            "\"external_agreement_no\":\"test20190701\"," .
//            "\"external_logon_id\":\"13852852877\"," .
//            "\"access_params\":{" .
//            "\"channel\":\"ALIPAYAPP\"" .
//            "      }," .
//            "\"sub_merchant\":{" .
//            "\"sub_merchant_id\":\"2088123412341234\"," .
//            "\"sub_merchant_name\":\"滴滴出行\"," .
//            "\"sub_merchant_service_name\":\"滴滴出行免密支付\"," .
//            "\"sub_merchant_service_description\":\"免密付车费，单次最高500\"" .
//            "      }," .
//            "\"period_rule_params\":{" .
//            "\"period_type\":\"DAY\"," .
//            "\"period\":3," .
//            "\"execute_time\":\"2019-01-23\"," .
//            "\"single_amount\":10.99," .
//            "\"total_amount\":1," .
//            "\"total_payments\":12" .
//            "      }" .
//            "    }" .
//            "  }");
        $aop->sdkExecute($reqObj);
//        $responseNode = str_replace(".", "_", $reqObj->getApiMethodName()) . "_response";
        return $this->toJson();
    }

    //接入支付
    public function pay(Request $request)
    {
        $token = $request->post('token', ''); //登录token
        $type = $request->post('type', 1); //1原创需求订单
        $orderno = $request->post('orderno', 0); //订单号
        $price = $this->db->orderPrice($type, $orderno);
        if (!$price) {
            return $this->toJson(-2, '价格错误');
        }
        $uOne = $this->db->userByToken($token);
        if (!$uOne) {
            return $this->toJson(-2, '未登录');
        }
        $aop = new \AopClient();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = $this->app_id;
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'utf-8';
        $aop->format = 'json';
        $payData = [
            'uid' => $uOne['id'],
            'out_trade_no' => $orderno,
            'money' => $price,
            'order_sign' => json_encode($aop, JSON_UNESCAPED_UNICODE)
        ];
        $lastid = $this->db->zfbPayAdd($payData);
        $reqObj = new \AlipayTradePagePayRequest();
        $reqData = json_encode([
            'body' => 'order',
            'out_trade_no' => (string) $orderno,
            'total_amount' => $price,
            'subject' => 'order',
            'product_code' => 'FAST_INSTANT_TRADE_PAY',
        ], JSON_UNESCAPED_UNICODE);
        $reqObj->setBizContent($reqData);
        $this->db->zfbPayModify($lastid, ['order_req' => $reqData]);
        $aop->pageExecute($reqObj);
        $responseNode = str_replace(".", "_", $reqObj->getApiMethodName()) . "_response";
        $backData = json_encode($responseNode, JSON_UNESCAPED_UNICODE);
        $this->db->zfbPayModify($lastid, ['order_back' => $backData]);
        return $this->toJson();
    }

    //支付宝通知
    public function notice(Request $request)
    {
        $zfb = $request->all();
        $zfb = json_encode($zfb, JSON_UNESCAPED_UNICODE);;
        file_put_contents('zfb.sql', $zfb);
    }
}